if ( typeof Essoft == 'undefined' ) {
	Essoft = {
		Version: '1.0.0'
    };
}

Essoft.Stash = {};

// Essoft.Utils class    
Essoft.Utils = Class.create();
Essoft.Utils.stirCards = function (ary) {
	for ( var i = 0; i < ary.length ; i++ ) {
		var rand = Math.round( Math.random() * (ary.length - 1) );
		var tmp_value = ary[i];
		ary[i] = ary[rand];
		ary[rand] = tmp_value;
	}
	return ary;
}

Essoft.Utils.makeGetPostRequest = function () {
	var args = Object.extend({
		url: undefined,
		method: 'get',
		status_200: undefined,
		async: false
	}, arguments[0]);
	
	var xhr;
	if ( window.XMLHttpRequest ) { // Mozilla, Safari,...
		xhr = new XMLHttpRequest();
		if ( xhr.overrideMimeType ) {
		// set type accordingly to anticipated content type
		// http_request.overrideMimeType('text/xml');
			xhr.overrideMimeType('text/css');
		}
	} 
	else if ( window.ActiveXObject ) { // IE
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	if ( ! xhr ) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}

	xhr.onreadystatechange = function () { 
		// controllo risposte
		if ( xhr.readyState == 1 ) {
			// alert(http_request.readyState);
		}
		else if ( xhr.readyState == 2 ) {
		}
		else if ( xhr.readyState == 3 ) {
			// alert(http_request.readyState);
		}	
		else if ( xhr.readyState == 4 ) {
			// alert(http_request.readyState);
			// alert(http_request.status);
			if ( xhr.status == 200 ) {
				if ( args.status_200 instanceof Function )
					args.status_200(xhr.responseText);
			}
			else {
				//alert('Non possibile effettuare la richiesta.');
			}
		}
	};
	//xhr.onreadystatechange = function() { alertContents(xhr) };

	try {
		xhr.open(args.method, args.url, args.async);
	//	http_request_array[numeroRequest].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//	http_request_array[numeroRequest].setRequestHeader("Content-length", parameters.length);
		xhr.setRequestHeader("Connection", "close");
		xhr.send();
		if ( ! args.async ) {
			if ( xhr.status == 200 || xhr.status == 0 )
				return xhr.responseText;
		}
	}
	catch (e) {}
}

Essoft.Utils.addStyle = function () {
	var args = Object.extend({
		document: document,
		id: 'lnp-css',
		url: null,
		src: null
	}, arguments[0]);

	var styleObj; 

	//	alert(args.document.getElementsByTagName('head')[0]);

	// Add ass src
	if ( args.src != undefined ) {
		// Create LINK object
		styleObj = args.document.createElement('LINK');
		styleObj.setAttribute('type', 'text/css');
		styleObj.setAttribute('id', args.id);
		styleObj.setAttribute('rel', 'stylesheet');
		styleObj.setAttribute('href', args.src);
	}
	// Load and insert inline
	else {
		// Create STYLE object
		styleObj = args.document.createElement('STYLE');
		styleObj.setAttribute('type', 'text/css');
		styleObj.setAttribute('id', args.id);
		
		// Check url
		if ( args.url != undefined ) {
			args.text = Essoft.Utils.makeGetPostRequest ({
		   		url: args.url,
	    		async: false
			});
		}

		if ( Prototype.Browser.IE ) 
			styleObj.styleSheet.cssText = args.text;
		else 
			styleObj.appendChild(args.document.createTextNode(args.text));	
	}

	// Add STYLE node to document HEAD section
	var headObj = args.document.getElementsByTagName('head')[0];
	//args.document.childNodes[1].childNodes[0];
	headObj.appendChild(styleObj);
}

Essoft.Utils.loadJS = function () {
	var args = Object.extend({
		document: document,
		id: 'prototype'
	}, arguments[0]);
 
	var script = args.document.createElement('script');
	script.setAttribute('type','text/javascript');
	script.setAttribute('id', args.id );
	
	// get js synchronously
	if ( args.src != undefined ) {
		args.js = Essoft.Utils.makeGetPostRequest ({
   			url: args.src,
		    async: false
		});
	}
	script.text = args.js;
	( args.document['getElementsByTagName']('head')[0] || args.document['getElementsByTagName']('body')[0] ).appendChild(script);
}